<template>
  <el-card class="box-card">
    <div slot="header" class="header">
      <span>销售类别占比</span>
      <el-radio-group v-model="value" size="medium" @change="handler">
        <el-radio-button label="全部渠道"></el-radio-button>
        <el-radio-button label="线上"></el-radio-button>
        <el-radio-button label="门店"></el-radio-button>
      </el-radio-group>
    </div>
    <div class="chart" ref="chart"></div>
  </el-card>
</template>

<script>
import * as echarts from "echarts";
export default {
  data() {
    return {
      value: "全部渠道",
      myChart: null,
    };
  },
  mounted() {
    this.myChart = echarts.init(this.$refs.chart);
    this.myChart.setOption({
      title: {
        text: "",
        subtext: "",
        left: "center",
        top: "center",
      },
      tooltip: {
        trigger: "item",
      },
      series: [
        {
          name: "Access From",
          type: "pie",
          //空心大小,图大小
          radius: ["40%", "80%"],
          avoidLabelOverlap: false,
          itemStyle: {
            borderColor: "#fff",
            borderWidth: 2,
          },
          label: {
            show: true,
            position: "outside",
          },
          labelLine: {
            show: true,
          },
          data: [
            { value: 244, name: "家用电器" },
            { value: 321, name: "食用酒水" },
            { value: 301, name: "个护健康" },
            { value: 41, name: "服饰箱包" },
            { value: 111, name: "母婴产品" },
            { value: 69, name: "其他" },
          ],
        },
      ],
    });
    this.myChart.on("mouseover", (params) => {
      const { name, value } = params.data;
      this.myChart.setOption({
        title: {
          text: name,
          subtext: value,
          left: "center",
          top: "center",
        },
      });
    });
  },
  methods: {
    handler(value) {
      if (value == "全部渠道") {
        this.myChart.setOption({
          series: [
            {
              data: [
                { value: 244, name: "家用电器" },
                { value: 321, name: "食用酒水" },
                { value: 301, name: "个护健康" },
                { value: 41, name: "服饰箱包" },
                { value: 111, name: "母婴产品" },
                { value: 69, name: "其他" },
              ],
            },
          ],
        });
      } else if (value == "线上") {
        this.myChart.setOption({
          series: [
            {
              data: [
                { value: 144, name: "家用电器" },
                { value: 231, name: "食用酒水" },
                { value: 201, name: "个护健康" },
                { value: 41, name: "服饰箱包" },
                { value: 322, name: "母婴产品" },
                { value: 45, name: "其他" },
              ],
            },
          ],
        });
      }else{
          this.myChart.setOption({
        series: [
        {
          data: [
             { value: 144, name: "家用电器" },
            { value: 121, name: "食用酒水" },
            { value: 301, name: "个护健康" },
            { value: 142, name: "服饰箱包" },
            { value: 61, name: "母婴产品" },
            { value: 69, name: "其他" },
          ],
        },
      ],
      })
      }
    },
  },
};
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #eee;
  align-items: center;
}
.chart {
  width: 100%;
  height: 300px;
}
</style>